<!-- 页面组件 页首 用户中心和首页使用-->
<template>
    <div class="header">
        <div class="header-container">
            <div class="app-name">
                <!-- 直接返回到index页面 --> 
                 <router-link to="/">
                    <img src="@/assets/03.png" class="logo" />
                </router-link>
                <b class="title">校园二手交易平台</b>
            </div>
            
            <!-- 搜索框 -->
            <div class="search-container">
                <!-- 键盘事件,keyup 按下按钮并释放时触发 -->
                <el-input placeholder="搜索" v-model="searchValue" @keyup.enter.native="searchIdle" size="large">
                    <el-button slot="append" @click="searchIdle">
                        <i class="el-icon-search" style="font-size: 24px;"> </i>
                    </el-button>
                </el-input>
            </div>

            <el-button type="primary" @click="toMessage"> 用户消息</el-button>
           
            <!-- 未登录状态,显示"登录"字样 -->
            <router-link v-if="!isLogin" class="user-name-text" to="/login"> 登录</router-link><!-- 建立链接，跳转路由 -->
            <!-- 已经登录状态 -->
            <div v-else class="user-container">
                <!-- 用户昵称 -->
                <div class="user-nickname">
                    <b>
                        {{ nicknameValue ? nicknameValue : nickname }}
                    </b>
                </div>
                <!-- 用户头像 -->
                <el-dropdown trigger="click">
                    <el-avatar :src="avatar" shape="square" :size="60"></el-avatar>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item>
                            <div @click="toMe">用户中心</div>
                        </el-dropdown-item>
                        <el-dropdown-item>
                            <div @click="toMerchant">商家中心</div>
                        </el-dropdown-item>
                        <el-dropdown-item style="color: red;">
                            <div @click="loginOut"> 退出登录</div>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'Header',
    props: ['searchInput', 'nicknameValue', 'avatarValue'],
    data() {
        return {
            searchValue: this.searchInput,
            nickname: '登录',
            avatar: '/avatar.jpg',
            isLogin: false
        };
    },
    created() {
        if (!this.$globalData.userInfo.nickname) {
            this.$api.getUserInfo().then(res => {
                console.log('Header getUserInfo:', res);
                if (res.status_code === 1) {
                    this.nickname = res.data.nickname;
                    this.avatar = this.$store.state.baseApi + res.data.avatar;
                    res.data.signInTime = res.data.signInTime.substring(0, 10);
                    this.$globalData.userInfo = res.data;
                    this.isLogin = true;
                }
            })
        } else {
            this.nickname = this.$globalData.userInfo.nickname;
            this.avatar = this.$store.state.baseApi + this.$globalData.userInfo.avatar;
            this.isLogin = true;
        }
    },
    methods: {
        searchIdle() {//搜索跳转
            if (this.$route.path !== '/search' ) {//当前路径不是search搜索路径，跳到搜索路径  searchValue是输入框的值，将他作为查询值
                this.$router.push({ path: '/search', query: { searchValue: this.searchValue } });//push跳转
            } else {
                this.$router.push({ path: '/search', query: { searchValue: this.searchValue } });
            }
        },
        goBack() {
                this.$router.go(-1);
        },
        toMe() {
            if (this.$route.path !== '/me' ) {
                this.$router.push({ path: '/me' });
            }
        },
        toMessage() {
            if ( this.$route.path !== '/message') {
                this.$router.push({ path: '/message' });
            }
        },
        toMerchant() {
            if (this.$route.path !== '/merchant' ) {
                this.$router.push({ path: '/merchant' });
            }
        },
        loginOut() {
            this.$api.logout().then(res => {
                if (res.status_code === 1) {//检查服务器返回的状态码，1代表登出成功
                    this.$globalData.userInfo = {};//清空全局用户信息
                    console.log("login out");
                    if ('/index' === this.$route.path) {//跳转到index
                        this.$router.go(0);
                    } else {
                        this.$router.push({ path: '/index' });//跳转到index也行
                    }
                } else {//登出失败
                    this.$message.error('网络或系统异常，退出登录失败！');
                }
            });
        }
    }
};
</script>
<style scoped>
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 70px;
    background: #ffffff;
    display: flex;
    justify-content: center;
    border-bottom: #eeeeee solid 2px;
    z-index: 1000;
}

.logo {
    width: 40px;
    height: 40px;
    margin: 5px 5px -5px 0;
}
.title {
    color: black;
    display: inline-block;
    margin-bottom: 20px;
    font-size: 28px;
    cursor: pointer;
}
.header-container {
    width: 1000px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.app-name a {
    color: #88c1fb;
    font-size: 24px;
    text-decoration: none;
}

.search-container {
    width: 300px;
}

.user-name-text {
    font-size: 16px;
    font-weight: 600;
    color: #91c8ff;
    cursor: pointer;
    text-decoration: none;
}
.user-container {
    cursor: pointer;
    display: flex;
    align-items: center;
    position: relative;
}

.user-nickname {
    font-size: 28px;
    color: #409EFF;
    padding-right: 5px;
}

</style>
